package fichier;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*;

import java.io.FileOutputStream;
import java.util.ArrayList;

/**

 */
public class PDF {
	
	private String nom_Client;
	private String nom_PDF;
	private Document document;
	private Client client;
	private Modele modele;
	private String[][] listeModele; 
	
	//constructeur du pdf list
	public PDF(Client c,String[][] donnee){
		this.client = c;
		this.listeModele = donnee;
        this.document = new Document();
        this.nom_Client = client.getNom();
        this.nom_PDF = new String("List_Modèles-"+this.nom_Client+".pdf");
        creationPDF_List();
    }
	
	//constructeur du pdf fiche
	public PDF(Modele m){
		this.modele = m;
		this.document = new Document();
		this.nom_PDF = new String("Fiche_Modèles-"+this.modele.getNumero_modele()+".pdf");
		creationPDF_Fiche();
	}
	
	public void creationPDF_List(){
		try {
            PdfWriter.getInstance(this.document,
                new FileOutputStream(this.nom_PDF));
    
            this.document.open();
            
            //logo entreprise
         /*   Image image = Image.getInstance("logo.png");
            image.scaleAbsoluteWidth(200);
            image.scaleAbsoluteHeight(80);
            image.setAbsolutePosition(370, 740);
            this.document.add(image);
           */ 
            //nom client et adresse
            this.document.add(new Paragraph(" "));
            this.document.add(new Paragraph(client.getNom()));
            this.document.add(new Paragraph(client.getAdresse1()));
            this.document.add(new Paragraph(client.getAdresse2()));
            this.document.add(new Paragraph(client.getCP()));
            this.document.add(new Paragraph(client.getVille()));
            this.document.add(new Paragraph(" "));
            
            //titre
            Font font1 = new Font(Font.FontFamily.HELVETICA  , 15, Font.BOLD);
            Paragraph pa = new Paragraph("Liste des modèles",font1);
            pa.setIndentationLeft(180);
            this.document.add(pa);
            pa= new Paragraph(" ");
            this.document.add(pa);
            this.document.add(pa);
            
            //tableau contenant la liste des modeles
            PdfPTable table = new PdfPTable(listeModele[0].length-1);
            float[] tabColonne = {100,100,100,80};
            table.setWidths(tabColonne);
            	//creation des cellules titres
            	Font font2 = new Font(Font.FontFamily.HELVETICA  , 13, Font.BOLD);
            	ArrayList<String> titre = new ArrayList<String>();
            	titre.add("     Designation");
            	titre.add("  Numero Modele");
            	titre.add("    Numero Plan");
            	titre.add("      Annee");
            	for(String str : titre){
            		PdfPCell cell = new PdfPCell();
            		cell.addElement(new Paragraph(str,font2));
            		cell.addElement(new Paragraph(" "));
    	            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
    	            cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
    	            cell.setBackgroundColor(new BaseColor(150,150,150));
    	            table.addCell(cell);
            	}
	            
	            //creation du contenu
            	for(int i=0; i<listeModele.length; i++){
            		
            		for(int j=1; j<listeModele[0].length; j++){
            			PdfPCell cell = new PdfPCell(new Paragraph(""));
            			cell.addElement(new Paragraph(this.listeModele[i][j]));
            			cell.addElement(new Paragraph(" "));
            			cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        	            cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        	            cell.setBackgroundColor(new BaseColor(255-(30*(i%2)),255-(30*(i%2)),255-(30*(i%2))));
        	            table.addCell(cell);
            		}
            		
            	}
            
            table.setWidthPercentage(85);
            this.document.add(table);

            
            
            this.document.close();
        } catch(Exception e){
        	
        }
	}
	
	public void creationPDF_Fiche(){
		try {
            PdfWriter.getInstance(this.document,
                new FileOutputStream(this.nom_PDF));
    
            this.document.open();
            
            //logo entreprise
         /*   Image image = Image.getInstance("logo.png");
            image.scaleAbsoluteWidth(200);
            image.scaleAbsoluteHeight(80);
            image.setAbsolutePosition(370, 740);
            this.document.add(image);
           */ 
            //titre
            Font fontT = new Font(Font.FontFamily.HELVETICA  , 15, Font.BOLD);
            Paragraph pa= new Paragraph(" ");
            this.document.add(pa);
            this.document.add(pa);
            this.document.add(pa);
            this.document.add(pa);
            pa = new Paragraph("Fiche modèle",fontT);
            pa.setIndentationLeft(180);
            this.document.add(pa);
            pa= new Paragraph(" ");
            this.document.add(pa);
            this.document.add(pa);
            
            PdfPCell cell = new PdfPCell();
            Font font1 = new Font(Font.FontFamily.HELVETICA  , 12, Font.BOLD);
            
            PdfPTable table1 = new PdfPTable(6);
            
            float[] tabColonne1 = {75,100,45,50,55,40};
            table1.setWidths(tabColonne1);
            
            cell = new PdfPCell(new Paragraph("Designation:",font1));
            cell.setBorder(Rectangle.NO_BORDER);
            cell.setPaddingBottom(15);
            table1.addCell(cell);
            cell = new PdfPCell(new Paragraph(""+this.modele.getDesignation()));
            cell.setBorder(Rectangle.NO_BORDER);
            cell.setPaddingBottom(15);
            table1.addCell(cell);
            cell = new PdfPCell(new Paragraph("Client:",font1));
            cell.setBorder(Rectangle.NO_BORDER);
            cell.setPaddingBottom(15);
            table1.addCell(cell);
            cell = new PdfPCell(new Paragraph(""+this.modele.getClient().getNom()));
            cell.setBorder(Rectangle.NO_BORDER);
            cell.setPaddingBottom(15);
            table1.addCell(cell);
            cell = new PdfPCell(new Paragraph("Nom Box:",font1));
            cell.setBorder(Rectangle.NO_BORDER);
            cell.setPaddingBottom(15);
            table1.addCell(cell);
            cell = new PdfPCell(new Paragraph(""+this.modele.getEmplacement().getNom()));
            cell.setBorder(Rectangle.NO_BORDER);
            cell.setPaddingBottom(15);
            table1.addCell(cell);
            cell = new PdfPCell(new Paragraph("N° Modèle:",font1));
            cell.setBorder(Rectangle.NO_BORDER);
            cell.setPaddingBottom(15);
            table1.addCell(cell);
            cell = new PdfPCell(new Paragraph(""+this.modele.getNumero_modele()));
            cell.setBorder(Rectangle.NO_BORDER);
            cell.setPaddingBottom(15);
            table1.addCell(cell);
            cell = new PdfPCell(new Paragraph("N° Plan:",font1));
            cell.setBorder(Rectangle.NO_BORDER);
            cell.setPaddingBottom(15);
            table1.addCell(cell);
            cell = new PdfPCell(new Paragraph(""+this.modele.getNumero_plan()));
            cell.setBorder(Rectangle.NO_BORDER);
            cell.setPaddingBottom(15);
            table1.addCell(cell);
            cell = new PdfPCell(new Paragraph(" "));
            cell.setBorder(Rectangle.NO_BORDER);
            cell.setPaddingBottom(15);
            table1.addCell(cell);
            table1.addCell(cell);
            this.document.add(table1);
            
            PdfPTable table3 = new PdfPTable(2);
            float[] tabColonne3 = {77,298};
            table3.setWidths(tabColonne3);
            cell = new PdfPCell(new Paragraph("Commentaire:",font1));
            cell.setBorder(Rectangle.NO_BORDER);
            cell.setPaddingBottom(15);
            table3.addCell(cell);
            cell = new PdfPCell(new Paragraph(""+this.modele.getCommentaire()));
            cell.setBorder(Rectangle.NO_BORDER);
            cell.setPaddingBottom(15);
            table3.addCell(cell);
            this.document.add(table3);
            
            PdfPTable table4 = new PdfPTable(4);
            float[] tabColonne4 = {170,100,170,100};
            table4.setWidths(tabColonne4);
            cell = new PdfPCell(new Paragraph("Nombre de Moules:",font1));
            cell.setBorder(Rectangle.NO_BORDER);
            cell.setPaddingBottom(15);
            table4.addCell(cell);
            cell = new PdfPCell(new Paragraph(""+this.modele.getNombre_moule()));
            cell.setBorder(Rectangle.NO_BORDER);
            cell.setPaddingBottom(15);
            table4.addCell(cell);
            cell = new PdfPCell(new Paragraph("Nombre de Boites:",font1));
            cell.setBorder(Rectangle.NO_BORDER);
            cell.setPaddingBottom(15);
            table4.addCell(cell);
            cell = new PdfPCell(new Paragraph(""+this.modele.getNombre_boite()));
            cell.setBorder(Rectangle.NO_BORDER);
            cell.setPaddingBottom(15);
            table4.addCell(cell);
            this.document.add(table4);
            
            this.document.close();
        } catch(Exception e){
        	
        }
	}
}